import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        // 这个会报错,是为了测试submit提交非form表单中
//        test03();
//        test04();
       // test05();
//        test06();
//        test07();
//        test08();
//        test09();
//        page01();
//        page02();
//        page03();
//        针对alert操作
        page04();
//        page05();
//        test10();
//        test11();
//        test12();
    }

    private static void test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D:\\img\\111.png"));
    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles 获取所有窗口句柄
        // 通过getWindowHandle 获取get打开的窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> windowHandles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:windowHandles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(4000);
//        webDriver.quit();
        webDriver.close();
    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/Data/mycodem/test/src/main/java/page/page5.html");
        webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\work\\image");
    }

    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:53750/test/page/page4.html?_ijt=i80vr5l19kjaeo04a28hds9d31&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("body > button")).click();
        sleep(3000);
        // alert弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        // 点击按钮
        webDriver.findElement(By.cssSelector("body > button")).click();
        // 在alert输入消息
        webDriver.switchTo().alert().sendKeys("哈哈哈");
        sleep(3000);
        // alert弹窗确认
        webDriver.switchTo().alert().accept();
    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:53750/test/page/page3.html?_ijt=kqcia4qtlla8p77unn98upaab0&_ij_reload=RELOAD_ON_SAVE");
        WebElement element = webDriver.findElement(By.cssSelector("#id"));
        Select select = new Select(element);
        //select.selectByIndex(3);
        select.selectByValue("5");
    }

    private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:53750/test/page/page2.html?_ijt=oupomf37to8169t74ki25flma6&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:53750/test/page/page1.html?_ijt=11f973aum9apjc9a75emh43ti9&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0;i < webElements.size();i++) {
            // 如果每个元素的type值等于checkbok就进行一个点金
            // getAttribute 得到属性值
            String type = webElements.get(i).getAttribute("type");
            if(type.equals("checkbox")) {
                webElements.get(i).click();
            }else {
                // 否则什么也不进行操作
            }

        }
    }

    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 找到图片连接
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        // 鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        //actions.contextClick(element).perform();
        actions.moveToElement(element).contextClick().perform();
    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        // control + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        // control + X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(3000);
        // control + V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");
        sleep(3000);

    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 浏览器后退
        webDriver.navigate().back();
        sleep(3000);
        webDriver.navigate().refresh();
        sleep(3000);
        webDriver.navigate().forward();
        sleep(3000);
        // 滑动条滑到最后
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test06() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver,1);
        //wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(9) > span")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));

    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前url: " + url);
            System.out.println("当前title :" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }

    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        String text = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (text.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        //webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).submit();
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();
    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        // 找到百度输入框,输入java
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
        // 点击了百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        //sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);

        // 清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }


    private static void test01() throws InterruptedException {
        int flag = 0;
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 找到百度输入框
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        // 输入软件测试
        element.sendKeys("软件测试");
        // 找到百度一下按钮
        // 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 效验
        // 找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0;i < elements.size();i++) {
            WebElement webElement = elements.get(i);
            //System.out.println(webElement.getText());
            // 如果返回的结果有软件测试,则测试通过,否则不通过
            if(webElement.getText().contains("软件") && webElement.getText().contains("测试")) {
                // 成功
            } else {
                flag = 1;
                System.out.println("测试失败");
                System.out.println(webElement.getText());
                break;
            }
        }
        System.out.println("测试成功");
    }
}
